package com.biz.primus.management.commodity.controller;

import com.biz.primus.commodity.vo.GroupProductDetailVo;
import com.biz.primus.commodity.vo.GroupProductVo;
import com.biz.primus.commodity.vo.backend.*;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.commodity.feign.GroupProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * 组合商品controller
 * @author wan
 * @create 2017-10-24-下午2:19
 */
@Controller
@RequestMapping("management/groupProductController")
public class GroupProductController extends AbstractBaseController{

    private final GroupProductFeignClient groupProductFeignClient;

    @Autowired
    public GroupProductController(GroupProductFeignClient groupProductFeignClient){
        this.groupProductFeignClient = groupProductFeignClient;
    }

    /**
     * 跳转到组合商品首页,并且查询数据
     * @return
     */
    @GetMapping(value = "goGroupProductMain")
    @ResponseBody
    public ModelAndView goGroupProductMain(@ModelAttribute("queryVo") GroupProductItemQueryVo queryVo){
        SpringPageVO<GroupProductItemItemVo> groupProductPage = groupProductFeignClient.findGroupProductList(queryVo);
        return new ModelAndView("groupproduct/list","pages",groupProductPage);
    }

    /**
     * 跳转-新增页面
     */
    @GetMapping(value = "goGroupProductAddForm")
    public ModelAndView goGroupProductAddForm(){
        //查询所有产品
        List<ProductVo> productList = groupProductFeignClient.findGroupProductSelectList();
        return new ModelAndView("groupproduct/detail").addObject("productList",productList).addObject("type","0");
    }

    /**
     * 跳转-编辑页面
     */
    @RequestMapping("goGroupProductEditForm")
    public ModelAndView goGroupProductEditForm(@RequestParam("id") String id){

        List<ProductVo> productList = groupProductFeignClient.findGroupProductSelectList();

        //需要组合商品下的所有子商品
        GroupProductVo groupProduct = groupProductFeignClient.findGroupProductById(id);

        String result = "";
        List<GroupProductVo> item = groupProduct.getItem();
        for (GroupProductVo groupProductVo : item) {
            result += groupProductVo.getProductId()+",";
        }

        ModelAndView view = new ModelAndView("groupproduct/detail").addObject("productList", productList).addObject("groupProduct", groupProduct);
        view.addObject("type","1");
        view.addObject("id",id);
        view.addObject("result",result.substring(0,result.length()-1));
        return view;
    }

    /**
     * 保存
     * @param groupProductVo
     * @param result
     * @return
     */
    @PostMapping("save")
    public ModelAndView save(@Valid GroupProductVo groupProductVo,BindingResult result){
        error(result);
        groupProductFeignClient.save(groupProductVo);
        return new ModelAndView("redirect:/management/groupProductController/goGroupProductMain");
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("del")
    public ModelAndView del(@RequestParam("id") String id){
        groupProductFeignClient.del(id);
        return new ModelAndView("redirect:/management/groupProductController/goGroupProductMain");
    }

}
